package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.icCard;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.Dic.CIcCardsInfoDic;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.Dic.IcCardAuthDic;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.IcCardInfoExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.TruckExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.IcCardAuthExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.CIcCardsInfoMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetIcCardAuthVo;
import com.xiaoxu.intranetweb.util.SzResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Component
@Slf4j
public class GetIcCardBu {
    @Autowired
    private ICIcCardsInfoService icCardsInfoService;
    @Autowired
    private ICIcCardsAuthService icCardsAuthService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ICCompanysService companysService;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ISysUserPlantConnService userPlantConnService;
    @Autowired
    private ISysUserRoleConnService userRoleConnService;
    @Autowired
    private ISysUserPlantConnService sysUserPlantConnService;
    @Autowired
    private ITPlantsService plantsService;
    @Autowired
    private ICTrucksService trucksService;
    @Autowired
    private ICTrucksUpdateService trucksUpdateService;
    ;
    @Autowired
    private ISysUserRoleConnService sysUserRoleConnService;
    @Autowired
    private CIcCardsInfoDic cIcCardsInfoDic;
    @Autowired
    private IcCardAuthDic IcCardAuthDic;
    @Autowired
    private ICProductsInfoService productsInfoService;
    @Autowired
    private CIcCardsInfoMapper icCardsInfoMapper;


    /**
     * 获取IC卡信息
     *
     * @param startTime 开始时间
     * @param icCardNo  卡号
     * @param co        客户名称
     * @param supplyCo  供应商
     * @param tranCo    运输商
     * @param truckNo   车牌号
     * @param cardType  卡类型{附卡(绑定车号)、附卡(临时灰卡)、附卡(绑定密码)、进货卡(绑定密码)、进货卡(绑定车号)}
     * @return
     */
    public SzResult getIcCard(String page, String pageSize, String token, String startTime, String endTime, String icCardNo,String cardinfAlia, String co,
                              String supplyCo, String tranCo, String truckNo, String cardType, String icCardStatus, String plantId) throws Exception {

        //如果不传page和pageSize则默认查询所有
        if (page == null || page.equals("") || pageSize == null || pageSize.equals("")) {
            List<CIcCardsInfo> list = icCardsInfoService.list();
            int total = list.size();
            Map<String, Object> map = new HashMap<>();
            map.put("total", total);
            map.put("list", list);
            return SzResult.success(map);
        }
        String chico = null;
        String suppluChico = null;
        String tranChico = null;
        if (co != null) {
            String[] split = co.split("\\.");
            co = split[0];
            chico = split[1];
        }
        if (supplyCo != null) {
            String[] split = supplyCo.split("\\.");
            supplyCo = split[0];
            suppluChico = split[1];
        }
        if (tranCo != null) {
            String[] split = tranCo.split("\\.");
            tranCo = split[0];
            tranChico = split[1];
        }
        Page<CIcCardsInfo> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq((icCardStatus != null && !icCardStatus.equals("")), "CARDINF_MODIFY_REASON", icCardStatus);
        queryWrapper.like(icCardNo != null && !icCardNo.equals(""), "CARDINF_CARDNO", icCardNo);
        queryWrapper.like(co != null && !co.equals(""), "CARDINF_CO", co);
        queryWrapper.like(chico != null && !chico.equals(""), "CARDINF_CHI_CO", chico);
        queryWrapper.like(supplyCo != null && !supplyCo.equals(""), "CARDINF_CO_SUPPLY", supplyCo);
        queryWrapper.like(suppluChico != null && !suppluChico.equals(""), "CARDINF_CHICO_SUPPLY", suppluChico);
        queryWrapper.like(tranCo != null && !tranCo.equals(""), "CARDINF_CO_TRAN", tranCo);
        queryWrapper.like(tranChico != null && !tranChico.equals(""), "CARDINF_CHICO_TRAN", tranChico);
        queryWrapper.like(truckNo != null && !truckNo.equals(""), "CARDINF_TRUCK_PWD", truckNo);
        queryWrapper.like(cardinfAlia != null && !cardinfAlia.equals(""), "CARDINF_ALIAS", cardinfAlia);
        queryWrapper.eq(cardType != null && !cardType.equals(""), "CARDINF_TYPE", cardType);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.between(startTime != null && !startTime.equals("") && endTime != null && !endTime.equals(""), "CARDINF_OPER_TIME", startTime, endTime);
        queryWrapper.orderByDesc("CARDINF_OPER_TIME");
        queryWrapper.eq("CARDINF_DELETED", 0);
        IPage<CIcCardsInfo> iPage = icCardsInfoService.page(page1, queryWrapper);
        List<CIcCardsInfo> list = iPage.getRecords();
//        for (int i = 0; i < list.size(); i++) {
//            CIcCardsInfo cIcCardsInfo = list.get(i);
//            String cardinfType = cIcCardsInfo.getCardinfType();
//            if (!cardinfType.contains("车号")) {
//                cIcCardsInfo.setCardinfTruckPwd("******");
//            }
//        }
        Long total = iPage.getTotal();
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("list", list);
        return SzResult.success(map);
    }


    //从user表里只获得司机的信息
    public SzResult getDriverFromUser() {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DEL_FLAG", 0);
        queryWrapper.select(SysUser.class, info -> !info.getColumn().equals("password") && !info.getColumn().equals("pwd_detail"));
        List<SysUser> list = sysUserService.list(queryWrapper);

        List<SysUser> driveList = new ArrayList<>();
        for (SysUser sysUser : list) {
            String id = sysUser.getId();
            QueryWrapper<SysUserRoleConn> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("USER_ID", id);
            SysUserRoleConn sysUserRoleConn = userRoleConnService.getOne(queryWrapper1);
            String roleId = sysUserRoleConn.getRoleId();
            if (roleId.equals("cb83f83bb9f1fc740ae3d5be1d3e4fe9")) {
                driveList.add(sysUser);
            }
        }
        return SzResult.success(driveList);
    }

    /**
     * 获取IC卡授权信息
     *
     * @param page      页码
     * @param pageSize  每页显示条数
     * @param endTime   结束时间
     * @param startTime 开始时间
     * @param icCardNo  卡号
     * @return
     */
    public SzResult getIcCardAuth(String page, String pageSize, String startTime, String endTime, String icCardNo, String plantId) {
        Page<CIcCardsAuth> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        QueryWrapper<CIcCardsAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(icCardNo != null && icCardNo.equals(""), "CARDAUTH_CARDNO", icCardNo);
        queryWrapper.eq("CARDAUTH_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.between(startTime != null && startTime.equals("") && endTime != null && endTime.equals(""), "CARDAUTH_CREATE_TIME", startTime, endTime);
        queryWrapper.orderByDesc("CARDAUTH_CREATE_TIME");
        IPage<CIcCardsAuth> iPage = icCardsAuthService.page(page1, queryWrapper);
        List<CIcCardsAuth> list = iPage.getRecords();
        Long total = (long) list.size();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        List<GetIcCardAuthVo> getIcCardAuthVos = new ArrayList<>();
        for (CIcCardsAuth cIcCardsAuth : list) {
            GetIcCardAuthVo getIcCardAuthVo = new GetIcCardAuthVo();
            getIcCardAuthVo.setId(cIcCardsAuth.getId());
            getIcCardAuthVo.setCardId(String.valueOf(cIcCardsAuth.getCardauthCardId()));
            getIcCardAuthVo.setCardNo(cIcCardsAuth.getCardauthCardno());
            getIcCardAuthVo.setDriverUserId(cIcCardsAuth.getCardauthDriverUserid());
            if (cIcCardsAuth.getCardauthDriverUserid() != null) {
                QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("ID", cIcCardsAuth.getCardauthDriverUserid());
                queryWrapper1.eq("DEL_FLAG", 0);
                SysUser sysUser = sysUserService.getOne(queryWrapper1);
                if (sysUser != null) {
                    getIcCardAuthVo.setDriverName(sysUser.getTrueName());
                    getIcCardAuthVo.setPhone(sysUser.getPhone());
                } else {
                    getIcCardAuthVo.setDriverName("");
                    getIcCardAuthVo.setPhone("");
                }
            } else {
                getIcCardAuthVo.setDriverName("");
                getIcCardAuthVo.setPhone("");
            }
            getIcCardAuthVo.setCreateTime(cIcCardsAuth.getCardauthCreateTime());
            getIcCardAuthVo.setStartTime(cIcCardsAuth.getCardauthStartTime());
            getIcCardAuthVo.setEndTime(cIcCardsAuth.getCardauthEndTime());
            getIcCardAuthVo.setCpyId(cIcCardsAuth.getCardauthCpyId());
            if (cIcCardsAuth.getCardauthCpyId() != null) {
                QueryWrapper<CCompanys> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("ID", cIcCardsAuth.getCardauthCpyId());
                queryWrapper2.eq("CPY_DELETED", 0);
                queryWrapper2.eq("DATA_FROM", plantId);
                CCompanys cCompanys = companysService.getOne(queryWrapper2);
                if (cCompanys != null) {
                    getIcCardAuthVo.setCpyName(cCompanys.getCpyCo() + "." + cCompanys.getCpyChico());
                } else {
                    getIcCardAuthVo.setCpyName("");
                }
            } else {
                getIcCardAuthVo.setCpyName("");
            }
            getIcCardAuthVo.setOperUserId(cIcCardsAuth.getCardauthOperUserid());
            if (cIcCardsAuth.getCardauthOperUserid() != null) {
                QueryWrapper<SysUser> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("ID", cIcCardsAuth.getCardauthOperUserid());
                queryWrapper3.eq("DEL_FLAG", 0);
                queryWrapper3.eq("DATA_FROM", plantId);
                SysUser sysUser1 = sysUserService.getOne(queryWrapper3);
                getIcCardAuthVo.setOperName(sysUser1.getTrueName());
            } else {
                getIcCardAuthVo.setOperName("");
            }
            getIcCardAuthVo.setState(cIcCardsAuth.getCardauthState());
            getIcCardAuthVo.setRevokeTime(cIcCardsAuth.getCardauthRevokeTime());

            getIcCardAuthVos.add(getIcCardAuthVo);
        }
        map.put("list", getIcCardAuthVos);
        return SzResult.success(map);
    }


    //新增IC卡授权信息   cardAuthCardNo 卡号000012 cardAuthDriverUserId 司机ID   cardAuthOperUserID 授权人ID  cardAuthState 授权状态（0正常，1撤销） cardAuthStartTime 开始时间  cardAuthEndTime 结束时间  cardAuthCpyId 公司ID
    public SzResult addIcCardAuth(String cardAuthCardNo, String cardAuthDriverUserId, String cardAuthCpyId,
                                  String cardAuthStartTime, String cardAuthEndTime, String token, String plantId) throws Exception {

        CIcCardsAuth cIcCardsAuth = new CIcCardsAuth();
        if (cardAuthCardNo == null || cardAuthCardNo.equals("")) {
            return SzResult.Error("卡号不能为空");
        }
        QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CARDINF_CARDNO", cardAuthCardNo);
        queryWrapper.eq("CARDINF_DELETED", 0);
        queryWrapper.ne("CARDINF_MODIFY_REASON", "注销");
        CIcCardsInfo cIcCardsInfo = icCardsInfoService.getOne(queryWrapper);
        if (cIcCardsInfo == null) {
            return SzResult.Error("该卡号不存在,请重新输入");
        }
        //ic卡存在
        //判断该卡号是否已经授权
//        QueryWrapper<CIcCardsAuth> queryWrapper1 = new QueryWrapper<>();
//        queryWrapper1.eq("CARDAUTH_CARDNO", cardAuthCardNo);
//        queryWrapper1.eq("CARDAUTH_DELETED", 0);
//        CIcCardsAuth cIcCardsAuth1 = icCardsAuthService.getOne(queryWrapper1);
//        if (cIcCardsAuth1 != null) {
//            return SzResult.error("该卡号已经授权,请重新输入");
//        }
        cIcCardsAuth.setCardauthCardno(cardAuthCardNo);


        if (cardAuthDriverUserId == null || cardAuthDriverUserId.equals("")) {
            return SzResult.Error("司机不能为空");
        }
        //判断是不是司机
        QueryWrapper<SysUser> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("ID", cardAuthDriverUserId);
        queryWrapper2.eq("DEL_FLAG", 0);
        SysUser sysUser = sysUserService.getOne(queryWrapper2);
        if (sysUser == null) {
            return SzResult.Error("该司机不存在或已被删除,请重新输入");
        }
        String id1 = sysUser.getId();//拿到USER的ID
        QueryWrapper<SysUserRoleConn> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("USER_ID", id1);
        SysUserRoleConn sysUserRoleConn = sysUserRoleConnService.getOne(queryWrapper3);
        String roleId = sysUserRoleConn.getRoleId();
        if (!roleId.equals("cb83f83bb9f1fc740ae3d5be1d3e4fe9")) {
            return SzResult.Error("该用户不是司机,请重新输入");
        }
        cIcCardsAuth.setCardauthDriverUserid(cardAuthDriverUserId);


        String cardinfType = cIcCardsInfo.getCardinfType();
        BigDecimal cardinfAgent = cIcCardsInfo.getCardinfAgent();
        if (Objects.equals(cardinfAgent, BigDecimal.valueOf(0))) {//不是代扣卡
            if (cardinfType.contains("附卡")) {//出货类绑定收货单位
                String cardinfCo = cIcCardsInfo.getCardinfCo();
                String cardinfChiCo = cIcCardsInfo.getCardinfChiCo();
                QueryWrapper<CCompanys> queryWrapper7 = new QueryWrapper<>();
                queryWrapper7.eq("CPY_CO", cardinfCo);
                queryWrapper7.eq("CPY_CHICO", cardinfChiCo);
                CCompanys cCompanys1 = companysService.getOne(queryWrapper7);
                if (cCompanys1 == null) {
                    return SzResult.Error("此卡绑定公司不存在或已被删除");
                }
                String id = cCompanys1.getId();
                BigDecimal bigDecimal = new BigDecimal(id);
                cIcCardsAuth.setCardauthCpyId(bigDecimal);
            }
            if (cardinfType.contains("进货卡")) {//进货类绑定供货单位
                String cardinfCoSupply = cIcCardsInfo.getCardinfCoSupply();
                String cardinfChiCoSupply = cIcCardsInfo.getCardinfChicoSupply();
                QueryWrapper<CCompanys> queryWrapper7 = new QueryWrapper<>();
                queryWrapper7.eq("CPY_CO", cardinfCoSupply);
                queryWrapper7.eq("CPY_CHICO", cardinfChiCoSupply);
                CCompanys cCompanys1 = companysService.getOne(queryWrapper7);
                String id = cCompanys1.getId();
                BigDecimal bigDecimal = new BigDecimal(id);
                cIcCardsAuth.setCardauthCpyId(bigDecimal);
            }
        } else {//代扣卡
            String cardinfCoTran = cIcCardsInfo.getCardinfCoTran();
            String cardinfChicoTran = cIcCardsInfo.getCardinfChicoTran();
            QueryWrapper<CCompanys> queryWrapper7 = new QueryWrapper<>();
            queryWrapper7.eq("CPY_CO", cardinfCoTran);
            queryWrapper7.eq("CPY_CHICO", cardinfChicoTran);
            CCompanys cCompanys1 = companysService.getOne(queryWrapper7);
            if (cCompanys1 == null) {
                return SzResult.Error("此卡绑定公司不存在或已被删除");
            }
            String id = cCompanys1.getId();
            BigDecimal bigDecimal = new BigDecimal(id);
            cIcCardsAuth.setCardauthCpyId(null);
        }

        cIcCardsAuth.setCardauthState(0);

        if (cardAuthStartTime == null || cardAuthStartTime.equals("")) {
            return SzResult.Error("开始时间不能为空");
        }
        String replace = cardAuthStartTime.replace(" ", "T");//将T替换为空格
        cIcCardsAuth.setCardauthStartTime(LocalDateTime.parse(replace));

        if (cardAuthEndTime == null || cardAuthEndTime.equals("")) {
            return SzResult.Error("结束时间不能为空");
        }
        String replace1 = cardAuthEndTime.replace(" ", "T");//将T替换为空格
        cIcCardsAuth.setCardauthEndTime(LocalDateTime.parse(replace1));

        cIcCardsAuth.setCardauthCreateTime(LocalDateTime.now());

        cIcCardsAuth.setCardauthDeleted(0);

        String cardInfId = String.valueOf(cIcCardsInfo.getCardinfId());
        cIcCardsAuth.setCardauthCardId(cardInfId);

        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String id = user.getId();

        QueryWrapper<TPlants> queryWrapper6 = new QueryWrapper<>();
        queryWrapper6.eq("ID", plantId);
        TPlants tPlants = plantsService.getOne(queryWrapper6);
        String id2 = tPlants.getId();
        cIcCardsAuth.setDataFrom(id2);
        cIcCardsAuth.setCardauthOperUserid(id);
        cIcCardsAuth.setCardauthDeleted(0);
        icCardsAuthService.save(cIcCardsAuth);
        sysLogBu.logRecord("新增IC卡授权信息", "新增", "新增卡号为" + cIcCardsAuth.getCardauthCardno() + "的IC卡授权信息", plantId);


        return SzResult.Success();
    }


    //删除IC卡授权信息
    public SzResult deleteIcCardAuth(String cardAuthId) {
        CIcCardsAuth cIcCardsAuth = icCardsAuthService.getById(cardAuthId);
        if (cIcCardsAuth == null) {
            return SzResult.Error("该授权信息不存在");
        }
        cIcCardsAuth.setCardauthDeleted(1);
        cIcCardsAuth.setDownedID1(0);
        icCardsAuthService.updateById(cIcCardsAuth);
        return SzResult.Success();
    }


    //修改IC卡授权信息
    public SzResult upDateIcCardAuth(String id, String cardAuthDriverUserId, String cardAuthCpyId, String cardAuthState,
                                     String cardAuthStartTime, String cardAuthEndTime, String token, String plantId) throws Exception {
        QueryWrapper<CIcCardsAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("CARDAUTH_DELETED", 0);
        CIcCardsAuth cIcCardsAuth = icCardsAuthService.getOne(queryWrapper);
        if (cIcCardsAuth == null) {
            return SzResult.Error("该卡号不存在,请重新输入");
        }
        if (cardAuthDriverUserId != null && !cardAuthDriverUserId.equals("")) {
            //判断是不是司机
            QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("ID", cardAuthDriverUserId);
            queryWrapper1.eq("DEL_FLAG", 0);
            SysUser sysUser = sysUserService.getOne(queryWrapper1);
            if (sysUser == null) {
                return SzResult.error("该司机不存在或已被删除,请重新输入");
            }
            String id1 = sysUser.getId();//拿到USER的ID
            QueryWrapper<SysUserRoleConn> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("USER_ID", id1);
            SysUserRoleConn sysUserRoleConn = sysUserRoleConnService.getOne(queryWrapper2);
            String roleId = sysUserRoleConn.getRoleId();
            if (!roleId.equals("cb83f83bb9f1fc740ae3d5be1d3e4fe9")) {
                return SzResult.error("该用户不是司机,请重新输入");
            }
            cIcCardsAuth.setCardauthDriverUserid(cardAuthDriverUserId);
        }
        if (cardAuthCpyId != null && !cardAuthCpyId.equals("")) {
            //判断是不是公司
            QueryWrapper<CCompanys> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("ID", cardAuthCpyId);
            queryWrapper2.eq("CPY_DELETED", 0);
            CCompanys cCompanys = companysService.getOne(queryWrapper2);
            if (cCompanys == null) {
                return SzResult.error("该公司不存在或已被删除,请重新输入");
            }
            cIcCardsAuth.setCardauthCpyId(BigDecimal.valueOf(Long.parseLong(cardAuthCpyId)));
        }
        if (cardAuthState != null && !cardAuthState.equals("")) {
            cIcCardsAuth.setCardauthState(Integer.valueOf(cardAuthState));
        }
        if (cardAuthStartTime != null && !cardAuthStartTime.equals("")) {
            cIcCardsAuth.setCardauthStartTime(LocalDateTime.parse(cardAuthStartTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        if (cardAuthEndTime != null && !cardAuthEndTime.equals("")) {
            cIcCardsAuth.setCardauthEndTime(LocalDateTime.parse(cardAuthEndTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        cIcCardsAuth.setDownedID1(0);
        icCardsAuthService.updateById(cIcCardsAuth);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        sysLogBu.logRecord("修改IC卡授权信息", user.getTrueName(), "修改卡号为" + cIcCardsAuth.getCardauthCardno() + "的IC卡授权信息", plantId);
        return SzResult.Success();
    }

    //新增IC卡信息
    public SzResult addIcCardInfo(String cardInfCardNo, String cardInfAgent, String cardInfCo, String cardInfChiCo, String cardInfCoSupply, String cardInfCoChiSupply,
                                  String cardInfCoTran, String cardInfCoChiTran, String cardInfPreferPdt, String cardInfType,
                                  String cardInfTruckPwd, String codeForContinue, String token, String plantId) throws Exception {
        String uuid =   java.util.UUID.randomUUID().toString().replace("-", "");
//        搜索这个车号已经有几个代理卡了，只能一张
//        QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("CARDINF_CARDNO", cardInfCardNo);
//        queryWrapper.eq("CARDINF_DELETED", 0);
//        queryWrapper.eq("CARDINF_CUR_VALID", 1);//
//        CIcCardsInfo cIcCardsInfo = icCardsInfoService.getOne(queryWrapper);
//        if (cIcCardsInfo != null) {
//            return SzResult.error("卡号重复, 已存在一张有效卡");
//        }
        List<CIcCardsInfo> cIcCardsInfos = icCardsInfoMapper.selectIcAgentByTruckNo(plantId,cardInfTruckPwd);

        if (cIcCardsInfos.size()>=1) {
            return  SzResult.Error("该车辆已经存在代理卡，请先注销或挂失其他代理卡再发放新的代理卡");
        }
        CIcCardsInfo cIcCardsInfo = new CIcCardsInfo();
//            cIcCardsInfo.setCardinfId(uuid);
        QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("CARDINF_ID");
        queryWrapper.last("limit 1");
        queryWrapper.eq("DATA_FROM", plantId);
        CIcCardsInfo cCardsInfoService = icCardsInfoService.getOne(queryWrapper);
        if (cCardsInfoService != null) {
            BigDecimal cardinfId = cCardsInfoService.getCardinfId();
            String cardinfId1 = String.valueOf((cardinfId.intValue() + 1));
            cIcCardsInfo.setCardinfId(BigDecimal.valueOf(Long.parseLong(cardinfId1)));
        } else {
            cIcCardsInfo.setCardinfId(BigDecimal.valueOf(1));
// cAshGateVideoBind.setAshgateId(uuid);
        }
            cIcCardsInfo.setId(uuid);
        if (cardInfCardNo != null && !cardInfCardNo.equals("")) {
            //判断卡号是否重复
            QueryWrapper<CIcCardsInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("CARDINF_CARDNO", cardInfCardNo);
            queryWrapper1.eq("CARDINF_DELETED", 0);
            queryWrapper1.eq("CARDINF_CUR_VALID", 1);//当前有效
            CIcCardsInfo cIcCardsInfo2 = icCardsInfoService.getOne(queryWrapper1);
            if (cIcCardsInfo2 != null) {
                return SzResult.error("卡号重复,已存在一张有效卡");
            }
            cIcCardsInfo.setCardinfCardno(cardInfCardNo);// 卡号
            cIcCardsInfo.setCardinfCurValid(1);// 当前有效
        } else {
            return SzResult.error("卡号不能为空");
        }
        if (cardInfAgent != null && !cardInfAgent.equals("")) {
            if (cardInfAgent.equals("1")) {
                cIcCardsInfo.setCardinfAgent(BigDecimal.valueOf(1));// 代扣卡
            } else if (cardInfAgent.equals("0")) {
                cIcCardsInfo.setCardinfAgent(BigDecimal.valueOf(0));// 不是代扣卡
            }
        }
        if (cardInfCo != null) {
            if (cardInfCoSupply != null) {
                if (cardInfCo.equals(cardInfCoSupply)) {
                    return SzResult.error("收货单位与供货单位不能相同");
                }
            }
        }

        if (cardInfCoTran != null && !cardInfCoTran.isEmpty()) {
            cIcCardsInfo.setCardinfCoTran(cardInfCoTran);// 运输单位         绑定c_companys表
        }
        if (cardInfCoChiTran != null && !cardInfCoChiTran.isEmpty()) {
            cIcCardsInfo.setCardinfChicoTran(cardInfCoChiTran);// 运输子单位
        }
        if (cardInfPreferPdt != null && !cardInfPreferPdt.isEmpty()) {//品种不为空
            QueryWrapper<CProductsInfo> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("ID", cardInfPreferPdt);
            queryWrapper2.eq("PDTINF_DELETED", 0);
            CProductsInfo cProductsInfo = productsInfoService.getOne(queryWrapper2);
            if (cProductsInfo == null) {
                return SzResult.error("该品种不存在或已被删除");
            }
            String pdtinfPdt = cProductsInfo.getPdtinfPdt();
            cIcCardsInfo.setCardinfPreferPdt(pdtinfPdt);//品种设置完成
            String pdtinfSubjectId = cProductsInfo.getPdtinfSubjectId();//品种绑定的单位
            QueryWrapper<CCompanys> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("ID", pdtinfSubjectId);
            queryWrapper3.eq("CPY_DELETED", 0);
            queryWrapper3.eq("DATA_FROM", plantId);
            CCompanys cCompanys = companysService.getOne(queryWrapper3);
            if (cardInfType.equals("附卡(绑定车号)") || cardInfType.equals("附卡(绑定密码)")) {//出货卡
                if (cCompanys == null) {
                    //该IC卡的品种绑定的单位不存在，直接存传入的单位
//                    cIcCardsInfo.setCardinfCoSupply(cardInfCoSupply);// 供货单位
//                    cIcCardsInfo.setCardinfChicoSupply(cardInfCoChiSupply);// 供货子单位
                } else {
                    cIcCardsInfo.setCardinfCoSupply(cCompanys.getCpyCo());//     供货单位
                    cIcCardsInfo.setCardinfChicoSupply(cCompanys.getCpyChico());// 供货子单位
                }
                cIcCardsInfo.setCardinfCo(cardInfCo);// 收货单位
                cIcCardsInfo.setCardinfChiCo(cardInfChiCo);// 收货子单位
            } else {//进货卡
                if (cCompanys == null) {
                    //该IC卡的品种绑定的单位不存在，直接存传入的单位
//                    cIcCardsInfo.setCardinfCo(cardInfCo);// 收货单位
//                    cIcCardsInfo.setCardinfChiCo(cardInfChiCo);// 收货子单位
                } else {
                    cIcCardsInfo.setCardinfCo(cCompanys.getCpyCo());//     供货单位
                    cIcCardsInfo.setCardinfChiCo(cCompanys.getCpyChico());// 供货子单位
                }
                cIcCardsInfo.setCardinfCoSupply(cardInfCoSupply);// 供货单位
                cIcCardsInfo.setCardinfChicoSupply(cardInfCoChiSupply);// 供货子单位
            }

        } else {
            if (cardInfCo != null && !cardInfCo.isEmpty()) {
                cIcCardsInfo.setCardinfCo(cardInfCo);// 收货单位
            }
            if (cardInfChiCo != null && !cardInfChiCo.isEmpty()) {
                cIcCardsInfo.setCardinfChiCo(cardInfChiCo);// 收货子单位
            }
            if (cardInfCoSupply != null && !cardInfCoSupply.isEmpty()) {
                cIcCardsInfo.setCardinfCoSupply(cardInfCoSupply);// 供货单位
            }
            if (cardInfCoChiSupply != null && !cardInfCoChiSupply.isEmpty()) {
                cIcCardsInfo.setCardinfChicoSupply(cardInfCoChiSupply);// 供货子单位
            }
        }


        if (cardInfType != null && !cardInfType.equals("")) {
            cIcCardsInfo.setCardinfType(cardInfType);// 卡片类型   附卡（绑定车号）、附卡（绑定密码）、进货卡（绑定车号）、进货卡（绑定密码）
        } else {
            return SzResult.error("卡片类型不能为空");
        }
     /*   if (cardInfModifyReason != null && !cardInfModifyReason.equals("")) {
            cIcCardsInfo.setCardinfModifyReason(cardInfModifyReason);// 修改原因,即卡片状态：发卡、解挂、注销
        }*/
        cIcCardsInfo.setCardinfModifyReason("发卡");
        cIcCardsInfo.setCardinfDeleted(0);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        cIcCardsInfo.setCardinfOperator(user.getTrueName());        // 操作人
        cIcCardsInfo.setCardinfOperTime(LocalDateTime.now());// 操作人操作时间


        if (cardInfTruckPwd != null && !cardInfTruckPwd.equals("")) {
            if (cardInfType.contains("车号")) {
                //判断车号是否存在
                QueryWrapper<CTrucks> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("TRUCKS_TRUCKNO", cardInfTruckPwd);
                queryWrapper2.eq("TRUCKS_DELETED", 0);
                CTrucks cTrucks = trucksService.getOne(queryWrapper2);
                if (cTrucks == null) {
                    return SzResult.error("车号不存在");
                }
                if (codeForContinue.equals("0")) {
                    QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
                    queryWrapper3.eq("CARDINF_TRUCK_PWD", cardInfTruckPwd);
                    queryWrapper3.eq("CARDINF_DELETED", 0);
                    List<CIcCardsInfo> cIcCardsInfo2 = icCardsInfoService.list(queryWrapper3);
                    if (!cIcCardsInfo2.isEmpty()) {//该车号已经绑定过ic卡
                        return SzResult.success("该车号已有绑定的IC卡，是否继续绑定？");
                    }
                }
            }

            if (cardInfType.contains("密码")) {
                //判断cardInfTruckPwd中不能有中文
                String regEx = "[\\u4e00-\\u9fa5]";
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(cardInfTruckPwd);
                if (m.find()) {
                    return SzResult.error("密码不能包含中文字符");
                }
            }
            cIcCardsInfo.setCardinfTruckPwd(cardInfTruckPwd);// 牌号/密码，根据卡类型CLASS分辨是牌号还是密码
        }

            cIcCardsInfo.setId(uuid);

        cIcCardsInfo.setDataFrom(plantId);
        cIcCardsInfo.setCardinfTare(BigDecimal.valueOf(0));
        cIcCardsInfo.setCardinfReportLost(0);
        cIcCardsInfo.setCardinfNew(1);
        cIcCardsInfo.setCardinfModifyTm(String.valueOf(LocalDateTime.now()));//最后一次修改日期
        sysLogBu.logRecord("新增IC卡信息", user.getTrueName(), "新增了IC卡号为【" + cardInfCardNo + "】的信息", plantId);
        cIcCardsInfo.setDownedID1(0);
        icCardsInfoService.save(cIcCardsInfo);
        return SzResult.Success();
    }

    //修改IC卡信息
    public SzResult updateIcCardInfo(String id,String cardAlias, String cardInfAgent, String cardInfCardNo, String cardInfCo, String cardInfChiCo, String cardInfCoSupply, String cardInfChiCoSupply,
                                     String cardInfCoTran, String cardInfChiCoTran, String cardInfPreferPdt, String cardInfType, String cardInfModifyReason,
                                     String cardInfTruckPwd, String token, String codeForContinue, boolean isNull, String plantId) throws Exception {
        if (cardInfTruckPwd !=null && !cardInfTruckPwd.equals("") && !cardInfTruckPwd.contains("*")){
            QueryWrapper<CIcCardsInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("CARDINF_TRUCK_PWD", cardInfTruckPwd);
            queryWrapper1.eq("CARDINF_AGENT",1);
            queryWrapper1.ne("CARDINF_DELETED", 1);
            queryWrapper1.eq("DATA_FROM", plantId);
            queryWrapper1.ne("ID",id);
            CIcCardsInfo cIcCardsInfo2 = icCardsInfoService.getOne(queryWrapper1);
            if (cIcCardsInfo2 != null) {
                return SzResult.error("该车号已经存在代理卡，卡号【"+cIcCardsInfo2.getCardinfCardno()+"】");
            }
        }

        if (id != null && !id.equals("")) {
            QueryWrapper<CIcCardsInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("ID", id);
            queryWrapper1.eq("CARDINF_DELETED", 0);
            queryWrapper1.eq("DATA_FROM", plantId);
            CIcCardsInfo cIcCardsInfo2 = icCardsInfoService.getOne(queryWrapper1);
            if (cIcCardsInfo2 == null) {
                return SzResult.error("该IC卡不存在或已被删除");
            }
            //磅单校验
            // String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
            QueryWrapper<CTrucks> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.eq("TRUCKS_CHECKIN_CARDNO", id);
            queryWrapper4.between("TRUCKS_QUEUE_STATUS", 1, 6);
            queryWrapper4.eq("TRUCKS_DELETED", 0);
            queryWrapper4.eq("DATA_FROM", plantId);
            List<CTrucks> cTruckss = trucksService.list(queryWrapper4);
            if (cTruckss.size() > 0) {
                return SzResult.error("该IC卡绑定的车辆在拉货（送货）,请稍后再试");
            }


            //如果该IC卡正在使用中，则不能修改
            QueryWrapper<CTrucks> trucksQueryWrapper = new QueryWrapper<>();
            trucksQueryWrapper.eq("TRUCKS_CHECKIN_CARDNO", cIcCardsInfo2.getCardinfCardno());
            trucksQueryWrapper.eq("TRUCKS_DELETED", 0);
            trucksQueryWrapper.ne("TRUCKS_QUEUE_STATUS", 0);
            trucksQueryWrapper.eq("DATA_FROM", plantId);
            CTrucks truck = trucksService.getOne(trucksQueryWrapper);
            QueryWrapper<CTrucksUpdate> truckUpdateQueryWrapper = new QueryWrapper<>();
            truckUpdateQueryWrapper.eq("TRUCKS_CHECKIN_CARDNO", cIcCardsInfo2.getCardinfCardno());
            truckUpdateQueryWrapper.eq("TRUCKS_DELETED", 0);
            truckUpdateQueryWrapper.ne("TRUCKS_QUEUE_STATUS", 0);
            truckUpdateQueryWrapper.eq("DATA_FROM", plantId);
            CTrucksUpdate cTrucksUpdate = trucksUpdateService.getOne(truckUpdateQueryWrapper);
            if (truck != null && cTrucksUpdate != null) {
                return SzResult.error("该IC卡正在使用中，不能修改");
            }


            if (cardInfAgent != null && !cardInfAgent.equals("")) {
                cIcCardsInfo2.setCardinfAgent(BigDecimal.valueOf(Long.parseLong(cardInfAgent)));// 代扣卡
            }

            if (cardInfCardNo != null && !cardInfCardNo.equals("")) {
                QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("CARDINF_CARDNO", cardInfCardNo);
                queryWrapper.eq("CARDINF_DELETED", 0);
                queryWrapper.ne("ID", id);//排除自己
                queryWrapper.ne("CARDINF_MODIFY_REASON", "注销");
                queryWrapper.eq("DATA_FROM", plantId);
                CIcCardsInfo cIcCardsInfo1 = icCardsInfoService.getOne(queryWrapper);
                if (cIcCardsInfo1 != null) {
                    return SzResult.error("卡号重复");
                }
                cIcCardsInfo2.setCardinfCardno(cardInfCardNo);// 卡号
            }

            if (cardInfCo != null && !cardInfCo.equals("")) {
                if (cardInfCoSupply != null && !cardInfCoSupply.equals("")) {
                    if (cardInfCo.equals(cardInfCoSupply)) {
                        return SzResult.error("收货单位与供货单位不能相同");
                    }
                }
            }
            if (cardInfCo != null) {
                cIcCardsInfo2.setCardinfCo(cardInfCo);// 收货单位                绑定c_companys表
            }
            if (cardInfChiCo != null) {
                cIcCardsInfo2.setCardinfChiCo(cardInfChiCo);// 收货子单位
            }
            if (cardInfCoSupply != null) {
                cIcCardsInfo2.setCardinfCoSupply(cardInfCoSupply);// 供货单位     绑定c_companys表
            }else {
                cIcCardsInfo2.setCardinfCoSupply("");
            }
            if (cardInfChiCoSupply != null) {
                cIcCardsInfo2.setCardinfChicoSupply(cardInfChiCoSupply);// 供货子单位
            }else{
                cIcCardsInfo2.setCardinfChicoSupply("");
            }
            if (cardInfCoTran != null) {
                cIcCardsInfo2.setCardinfCoTran(cardInfCoTran);// 运输单位         绑定c_companys表
            }
            if (cardInfChiCoTran != null) {
                cIcCardsInfo2.setCardinfChicoTran(cardInfChiCoTran);// 运输子单位
            }
            if (cardInfPreferPdt != null && !cardInfPreferPdt.equals("")) {
                cIcCardsInfo2.setCardinfPreferPdt(cardInfPreferPdt);// 绑定品种    绑定c_products_info表
            }
            if (cardInfType != null && !cardInfType.equals("")) {
                cIcCardsInfo2.setCardinfType(cardInfType);// 卡片类型   附卡（绑定车号）、附卡（绑定密码）、进货卡（绑定车号）、进货卡（绑定密码）
            } else {
                return SzResult.error("卡片类型不能为空");
            }
//            卡号别名还不能重复
//            if (!Objects.equals(cIcCardsInfo2.getCardinfAlias(), cardAlias) && cardAlias != null && !cardAlias.equals("")) {
//                QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
//                queryWrapper3.eq("CARDINF_ALIAS", cardAlias);
//                queryWrapper3.eq("CARDINF_DELETED", 0);
//                queryWrapper3.ne("ID", id);
//                queryWrapper3.eq("DATA_FROM", plantId);
//                CIcCardsInfo cIcCardsInfo1 = icCardsInfoService.getOne(queryWrapper3);
//                if (cIcCardsInfo1 != null) {
//                    return SzResult.error("卡号别名重复,改别名已在卡号【"+cIcCardsInfo1.getCardinfCardno()+"】上使用");
//                }
//            }
            cIcCardsInfo2.setCardinfAlias(cardAlias);
//            cIcCardsInfo2.setCardinfModifyTm(String.valueOf(LocalDateTime.now()));//最后一次修改日期
            if (cardInfModifyReason != null && !cardInfModifyReason.equals("")) {
                //注销卡的状态不可修改为其他状态
                String cardinfModifyReason = cIcCardsInfo2.getCardinfModifyReason();
                if (cardinfModifyReason.equals("注销")) {
                    return SzResult.error("该卡已注销，不能修改状态");
                }
                if (cardInfModifyReason.equals("注销")) {
//                   cardinf_cur_valid为1为有效卡
                    cIcCardsInfo2.setCardinfCurValid(0);// 当前卡状态
                    cIcCardsInfo2.setCardinfDeleted(1);//注销就直接删掉卡
                    cIcCardsInfo2.setCardinfModifyReason("注销");// 修改原因,即卡片状态：发卡、挂失、解挂、注销（我认为的）
                }
                if (cardInfModifyReason.equals("发卡")) {
//                 先检查是否有重复的卡号且是有效卡
                    QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("CARDINF_CARDNO", cardInfCardNo);
                    queryWrapper.eq("CARDINF_DELETED", 0);
                    queryWrapper.ne("ID", id);//排除自己
                    queryWrapper.eq("CARDINF_CUR_VALID", 1);
                    queryWrapper.eq("DATA_FROM", plantId);
                    List<CIcCardsInfo> cIcCardsInfos = icCardsInfoService.list(queryWrapper);
                    if (cIcCardsInfos.size() > 0) {
                        return SzResult.error("此卡号当前已存在有效卡，修改失败");
                    }
                    cIcCardsInfo2.setCardinfCurValid(1);// 当前卡状态
                    cIcCardsInfo2.setCardinfModifyReason("发卡");// 修改原因,即卡片状态：发卡、挂失、解挂、注销（我认为的）
                }
                if (cardInfModifyReason.equals("挂失")) {
                    cIcCardsInfo2.setCardinfReportLost(1);// 挂失
                    cIcCardsInfo2.setCardinfCurValid(0);// 当前卡状态设为无效卡
                    cIcCardsInfo2.setCardinfModifyReason("挂失");// 修改原因,即卡片状态：发卡、挂失、解挂、注销（我认为的）
                }
                if (cardInfModifyReason.equals("解挂")) {
                    if (!cardinfModifyReason.equals("解挂")) {
                        //只能从挂失状态解挂
                        if (cIcCardsInfo2.getCardinfReportLost() != 1) {
                            return SzResult.error("该IC卡未挂失,无需解挂");
                        }
//                    解挂时，先检查是否有重复的卡号且是有效卡
                        QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("CARDINF_CARDNO", cardInfCardNo);
                        queryWrapper.eq("CARDINF_DELETED", 0);
                        queryWrapper.ne("ID", id);//排除自己
                        queryWrapper.eq("CARDINF_CUR_VALID", 1);
                        queryWrapper.eq("DATA_FROM", plantId);
                        List<CIcCardsInfo> cIcCardsInfos = icCardsInfoService.list(queryWrapper);
                        if (cIcCardsInfos.size() > 0) {
                            return SzResult.error("此卡号当前已存在有效卡，修改失败");
                        }
                        cIcCardsInfo2.setCardinfReportLost(0);// 解挂
                        cIcCardsInfo2.setCardinfCurValid(1);// 当前卡状态
                        cIcCardsInfo2.setCardinfModifyReason("解挂");// 修改原因,即卡片状态：发卡、挂失、解挂、注销（我认为的）
                    }
                }
            }
            if (cardInfTruckPwd != null && !cardInfTruckPwd.equals("")) {
                if (cardInfType.contains("车号")) {
                    String cardinfTruckPwd = cIcCardsInfo2.getCardinfTruckPwd();
                    if (cardinfTruckPwd != null && !cardinfTruckPwd.equals("")) {
                        if (!cardinfTruckPwd.equals(cardInfTruckPwd)) {
                            //判断车号是否存在
                            QueryWrapper<CTrucks> queryWrapper2 = new QueryWrapper<>();
                            queryWrapper2.eq("TRUCKS_TRUCKNO", cardInfTruckPwd);
                            queryWrapper2.eq("TRUCKS_DELETED", 0);
                            queryWrapper2.eq("DATA_FROM", plantId);
                            CTrucks cTrucks = trucksService.getOne(queryWrapper2);
                            if (cTrucks == null) {
                                return SzResult.error("车号不存在");
                            }
                            if (codeForContinue.equals("0")) {
                                QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
                                queryWrapper3.eq("CARDINF_TRUCK_PWD", cardInfTruckPwd);
                                queryWrapper3.eq("CARDINF_DELETED", 0);
                                queryWrapper3.eq("DATA_FROM", plantId);
                                List<CIcCardsInfo> cIcCardsInfo3 = icCardsInfoService.list(queryWrapper3);
                                if (cIcCardsInfo3 != null) {//该车号已经绑定过ic卡
                                    return SzResult.success("该车号已有绑定的IC卡，是否继续绑定？");
                                }
                            }
                        }
                    }
                }
                if (cardInfType.contains("密码")) {
                    //判断cardInfTruckPwd中不能有中文
                    String regEx = "[\\u4e00-\\u9fa5]";
                    Pattern p = Pattern.compile(regEx);
                    Matcher m = p.matcher(cardInfTruckPwd);
                    if (m.find()) {
                        return SzResult.error("密码不能包含中文字符");
                    }
                }
                cIcCardsInfo2.setCardinfTruckPwd(cardInfTruckPwd);// 牌号/密码，根据卡类型CLASS分辨是牌号还是密码
            }
//            cIcCardsInfo2.setCardinfModifyTm(String.valueOf(LocalDateTime.now()));//最后一次修改日期
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            cIcCardsInfo2.setCardinfOperator(user.getTrueName());        // 操作人
            cIcCardsInfo2.setCardinfOperTime(LocalDateTime.now());// 操作人操作时间
//            int a;
//            if (isNull) {
//                cIcCardsInfo2.setCardinfModifyTmNum(BigDecimal.valueOf(0));
//            } else {
//                a = cIcCardsInfo2.getCardinfModifyTmNum().intValue();
//                cIcCardsInfo2.setCardinfModifyTmNum(BigDecimal.valueOf(a + 1));
//            }
            cIcCardsInfo2.setDownedID1(0);
            icCardsInfoService.updateById(cIcCardsInfo2);
            sysLogBu.logRecord("修改IC卡信息", user.getTrueName(), "修改了IC卡号为" + cIcCardsInfo2.getCardinfCardno() + "的IC卡信息", plantId);
            return SzResult.Success();
        }else {
            return SzResult.error("无效卡号");
        }

    }

    //删除IC卡信息
    public SzResult deleteIcCardInfo(String id, String token, String plantId) throws Exception {
        //  String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TRUCKS_CHECKIN_CARDNO", id);
        queryWrapper.between("TRUCKS_QUEUE_STATUS", 1, 6);
        queryWrapper.eq("TRUCKS_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        List<CTrucks> cTrucks = trucksService.list(queryWrapper);
        if (cTrucks.size() > 0) {
            return SzResult.error("该IC卡绑定的车辆在拉货（送货）,请稍后再试");
        }
        if (id != null && !id.equals("")) {
            QueryWrapper<CIcCardsInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("ID", id);
            queryWrapper1.eq("CARDINF_DELETED", 0);
            CIcCardsInfo cIcCardsInfo2 = icCardsInfoService.getOne(queryWrapper1);
            if (cIcCardsInfo2 == null) {
                return SzResult.error("该IC卡不存在或已被删除");
            }
            cIcCardsInfo2.setCardinfDeleted(1);
            cIcCardsInfo2.setDownedID1(0);
            icCardsInfoService.updateById(cIcCardsInfo2);
        }
        return SzResult.Success();
    }

    //导出ic卡信息excel
    @Async
    public Future<List<IcCardInfoExcelEn>> exportExcel(List<Map<String, Object>> fields, String filename, List<Map<String, Object>> ids, Boolean isHeader, String mode, Boolean original, Map<String, String> pager, String sheetName, String token, String startTime, String endTime, String icCardNo, String co,
                                                       String supplyCo, String tranCo, String truckNo, String cardType, String icCardStatus, String plantId) {
        List<Map<String, Object>> newIds = new ArrayList<>();
//        最终数据
        List<IcCardInfoExcelEn> list = new ArrayList<>();
        try {
            switch (mode) {
                case "current":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
//             newIds为Map<String, Object>类型,需要转换为TruckExcelEn类型
                    for (Map<String, Object> newId : newIds) {
                        IcCardInfoExcelEn icCardInfoExcelEn = IcCardInfoExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            if (entry.getKey().equals("cardinfCardno")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfCardno("");
                                } else {
                                    icCardInfoExcelEn.setCardinfCardno(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardAlias")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardAlias("");
                                } else {
                                    icCardInfoExcelEn.setCardAlias(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfTruckPwd")) {
                                if (entry.getValue().toString().equals("")) {
                                    icCardInfoExcelEn.setCardinfTruckPwd("");
                                } else {
                                    icCardInfoExcelEn.setCardinfTruckPwd(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfAgent")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfAgent("");
                                } else {
                                    icCardInfoExcelEn.setCardinfAgent(cIcCardsInfoDic.getCardinfAgent(Integer.valueOf(entry.getValue().toString())));
                                }
                            }
                            if (entry.getKey().equals("cardinfCo")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfCo("");
                                } else {
                                    icCardInfoExcelEn.setCardinfCo(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfChiCo")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfChiCo("");
                                } else {
                                    icCardInfoExcelEn.setCardinfChiCo(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfCoSupply")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfCoSupply("");
                                } else {
                                    icCardInfoExcelEn.setCardinfCoSupply(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfChicoSupply")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfChicoSupply("");
                                } else {
                                    icCardInfoExcelEn.setCardinfChicoSupply(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfCoTran")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfCoTran("");
                                } else {
                                    icCardInfoExcelEn.setCardinfCoTran(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfChicoTran")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfChicoTran("");
                                } else {
                                    icCardInfoExcelEn.setCardinfChicoTran(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfPreferPdt")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfPreferPdt("");
                                } else {
                                    icCardInfoExcelEn.setCardinfPreferPdt(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfType")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfType("");
                                } else {
                                    icCardInfoExcelEn.setCardinfType(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfModifyTm")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfModifyTm("");
                                } else {
                                    icCardInfoExcelEn.setCardinfModifyTm(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfModifyReason")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfModifyReason("");
                                } else {
                                    icCardInfoExcelEn.setCardinfModifyReason(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfOperator")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfOperator("");
                                } else {
                                    icCardInfoExcelEn.setCardinfOperator(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfOperTime")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfOperTime("");
                                } else {
                                    icCardInfoExcelEn.setCardinfOperTime(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfExcuseTare")) {//是否免过皮重
                                if (entry.getValue() == null || entry.getValue().equals("")) {

                                    icCardInfoExcelEn.setCardinfExcuseTare("");
                                } else {
                                    icCardInfoExcelEn.setCardinfExcuseTare(cIcCardsInfoDic.getCardinfExcuseTare(Integer.valueOf(entry.getValue().toString())));
                                }
                            }
                            if (entry.getKey().equals("cardinfTare")) {//免过多少吨
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfTare("");
                                } else {
                                    icCardInfoExcelEn.setCardinfTare(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfPutouted")) {//是否发放
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfPutouted("");
                                } else {
                                    icCardInfoExcelEn.setCardinfPutouted(cIcCardsInfoDic.getCardinfPutouted(Integer.valueOf(entry.getValue().toString())));
                                }
                            }
                            if (entry.getKey().equals("cardinfReportLost")) {//是否挂失
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfReportLost("");
                                } else {
                                    icCardInfoExcelEn.setCardinfReportLost(cIcCardsInfoDic.getCardinfReportLost(Integer.valueOf(entry.getValue().toString())));
                                }
                            }
                            if (entry.getKey().equals("cardinfCurValid")) {//卡片是否有效
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfCurValid("");
                                } else {
                                    icCardInfoExcelEn.setCardinfCurValid(cIcCardsInfoDic.getCardinfCurValid(Integer.valueOf(entry.getValue().toString())));
                                }
                            }
                        }
                        list.add(icCardInfoExcelEn);
                    }
                    break;
                case "all":
                    QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq((icCardStatus != null && !icCardStatus.equals("")), "CARDINF_MODIFY_REASON", icCardStatus);
                    queryWrapper.like(icCardNo != null && !icCardNo.equals(""), "CARDINF_CARDNO", icCardNo);
                    queryWrapper.like(co != null && !co.equals(""), "CARDINF_CO", co);
                    queryWrapper.like(supplyCo != null && !supplyCo.equals(""), "CARDINF_CO_SUPPLY", supplyCo);
                    queryWrapper.like(tranCo != null && !tranCo.equals(""), "CARDINF_CO_TRAN", tranCo);
                    queryWrapper.like(truckNo != null && !truckNo.equals(""), "CARDINF_TRUCK_PWD", truckNo);
                    queryWrapper.eq(cardType != null && !cardType.equals(""), "CARDINF_TYPE", cardType);
                    queryWrapper.between(startTime != null && !startTime.equals("") && endTime != null && !endTime.equals(""), "CARDINF_OPER_TIME", startTime, endTime);
                    queryWrapper.eq("DATA_FROM", plantId);
                    queryWrapper.orderByDesc("CARDINF_OPER_TIME");
                    queryWrapper.eq("CARDINF_DELETED", 0);
                    List<CIcCardsInfo> cIcCardsInfos = icCardsInfoService.list(queryWrapper);
                    for (CIcCardsInfo cIcCardsInfo : cIcCardsInfos) {
                        IcCardInfoExcelEn icCardInfoExcelEn = IcCardInfoExcelEn.builder()
//                            .trucksTruckno(Optional.ofNullable(cTruck.getTrucksTruckno()).orElse(""))
                                .cardinfExcuseTare(Optional.ofNullable(cIcCardsInfoDic.getCardinfExcuseTare(cIcCardsInfo.getCardinfExcuseTare())).map(Object::toString).orElse(""))
                                .cardinfModifyReason(Optional.ofNullable(cIcCardsInfo.getCardinfModifyReason()).orElse(""))
                                .cardinfModifyTm(Optional.ofNullable(cIcCardsInfo.getCardinfModifyTm()).orElse(""))
                                .cardinfOperator(Optional.ofNullable(cIcCardsInfo.getCardinfOperator()).orElse(""))
                                .cardinfOperTime(Optional.ofNullable(cIcCardsInfo.getCardinfOperTime()).map(Object::toString).orElse(""))
                                .cardinfPutouted(Optional.ofNullable(cIcCardsInfoDic.getCardinfPutouted(cIcCardsInfo.getCardinfPutouted())).map(Object::toString).orElse(""))
                                .cardinfReportLost(Optional.ofNullable(cIcCardsInfoDic.getCardinfReportLost(cIcCardsInfo.getCardinfReportLost())).map(Object::toString).orElse(""))
                                .cardinfTare(Optional.ofNullable(cIcCardsInfo.getCardinfTare()).map(Object::toString).orElse(""))
                                .cardinfCurValid(Optional.ofNullable(cIcCardsInfoDic.getCardinfCurValid(cIcCardsInfo.getCardinfCurValid())).map(Object::toString).orElse(""))
                                .cardinfCardno(Optional.ofNullable(cIcCardsInfo.getCardinfCardno()).orElse(""))
                                .cardinfCo(Optional.ofNullable(cIcCardsInfo.getCardinfCo()).orElse(""))
                                .cardinfCoSupply(Optional.ofNullable(cIcCardsInfo.getCardinfCoSupply()).orElse(""))
                                .cardinfCoTran(Optional.ofNullable(cIcCardsInfo.getCardinfCoTran()).orElse(""))
                                .cardinfType(Optional.ofNullable(cIcCardsInfo.getCardinfType()).orElse(""))
//                            cardinfModifyTm
                                .cardinfAgent(Optional.ofNullable(cIcCardsInfoDic.getCardinfAgent(Integer.parseInt(String.valueOf(cIcCardsInfo.getCardinfAgent())))).map(Object::toString).orElse(""))
                                .cardinfChiCo(Optional.ofNullable(cIcCardsInfo.getCardinfChiCo()).map(Object::toString).orElse(""))
                                .cardinfChicoSupply(Optional.ofNullable(cIcCardsInfo.getCardinfChicoSupply()).map(Object::toString).orElse(""))
                                .cardinfChicoTran(Optional.ofNullable(cIcCardsInfo.getCardinfChicoTran()).map(Object::toString).orElse(""))
                                .cardinfPreferPdt(Optional.ofNullable(cIcCardsInfo.getCardinfPreferPdt()).map(Object::toString).orElse(""))
                                .cardinfTruckPwd(Optional.ofNullable(cIcCardsInfo.getCardinfTruckPwd()).map(Object::toString).orElse(""))
                                .cardAlias(Optional.ofNullable(cIcCardsInfo.getCardinfAlias()).map(Object::toString).orElse(""))
                                .build();
                        list.add(icCardInfoExcelEn);
                    }
                    break;
                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
//             newIds为Map<String, Object>类型,需要转换为TruckExcelEn类型
                    for (Map<String, Object> newId : newIds) {
                        IcCardInfoExcelEn icCardInfoExcelEn = IcCardInfoExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            if (entry.getKey().equals("cardinfCardno")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfCardno("");
                                } else {
                                    icCardInfoExcelEn.setCardinfCardno(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardAlias")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardAlias("");
                                } else {
                                    icCardInfoExcelEn.setCardAlias(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfTruckPwd")) {
                                if (entry.getValue().toString().equals("")) {
                                    icCardInfoExcelEn.setCardinfTruckPwd("");
                                } else {
                                    icCardInfoExcelEn.setCardinfTruckPwd(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfAgent")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfAgent("");
                                } else {
                                    icCardInfoExcelEn.setCardinfAgent(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfCo")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfCo("");
                                } else {
                                    icCardInfoExcelEn.setCardinfCo(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfChiCo")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfChiCo("");
                                } else {
                                    icCardInfoExcelEn.setCardinfChiCo(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfCoSupply")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfCoSupply("");
                                } else {
                                    icCardInfoExcelEn.setCardinfCoSupply(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfChicoSupply")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfChicoSupply("");
                                } else {
                                    icCardInfoExcelEn.setCardinfChicoSupply(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfCoTran")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfCoTran("");
                                } else {
                                    icCardInfoExcelEn.setCardinfCoTran(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfChicoTran")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfChicoTran("");
                                } else {
                                    icCardInfoExcelEn.setCardinfChicoTran(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfPreferPdt")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfPreferPdt("");
                                } else {
                                    icCardInfoExcelEn.setCardinfPreferPdt(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cardinfType")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    icCardInfoExcelEn.setCardinfType("");
                                } else {
                                    icCardInfoExcelEn.setCardinfType(entry.getValue().toString());
                                }
                                if (entry.getKey().equals("cardinfModifyTm")) {
                                    if (entry.getValue() == null || entry.getValue().equals("")) {
                                        icCardInfoExcelEn.setCardinfModifyTm("");
                                    } else {
                                        icCardInfoExcelEn.setCardinfModifyTm(entry.getValue().toString());
                                    }
                                }
                                if (entry.getKey().equals("cardinfModifyReason")) {
                                    if (entry.getValue() == null || entry.getValue().equals("")) {
                                        icCardInfoExcelEn.setCardinfModifyReason("");
                                    } else {
                                        icCardInfoExcelEn.setCardinfModifyReason(entry.getValue().toString());
                                    }
                                }
                                if (entry.getKey().equals("cardinfOperator")) {
                                    if (entry.getValue() == null || entry.getValue().equals("")) {
                                        icCardInfoExcelEn.setCardinfOperator("");
                                    } else {
                                        icCardInfoExcelEn.setCardinfOperator(entry.getValue().toString());
                                    }
                                }
                                if (entry.getKey().equals("cardinfOperTime")) {
                                    if (entry.getValue() == null || entry.getValue().equals("")) {
                                        icCardInfoExcelEn.setCardinfOperTime("");
                                    } else {
                                        icCardInfoExcelEn.setCardinfOperTime(entry.getValue().toString());
                                    }
                                }
                                if (entry.getKey().equals("cardinfExcuseTare")) {
                                    if (entry.getValue() == null || entry.getValue().equals("")) {
                                        icCardInfoExcelEn.setCardinfExcuseTare("");
                                    } else {
                                        icCardInfoExcelEn.setCardinfExcuseTare(entry.getValue().toString());
                                    }
                                }
                                if (entry.getKey().equals("cardinfTare")) {
                                    if (entry.getValue() == null || entry.getValue().equals("")) {
                                        icCardInfoExcelEn.setCardinfTare("");
                                    } else {
                                        icCardInfoExcelEn.setCardinfTare(entry.getValue().toString());
                                    }
                                }
                                if (entry.getKey().equals("cardinfPutouted")) {
                                    if (entry.getValue() == null || entry.getValue().equals("")) {
                                        icCardInfoExcelEn.setCardinfPutouted("");
                                    } else {
                                        icCardInfoExcelEn.setCardinfPutouted(entry.getValue().toString());
                                    }
                                }
                                if (entry.getKey().equals("cardinfReportLost")) {
                                    if (entry.getValue() == null || entry.getValue().equals("")) {
                                        icCardInfoExcelEn.setCardinfReportLost("");
                                    } else {
                                        icCardInfoExcelEn.setCardinfReportLost(entry.getValue().toString());
                                    }
                                }
                                if (entry.getKey().equals("cardinfCurValid")) {
                                    if (entry.getValue() == null || entry.getValue().equals("")) {
                                        icCardInfoExcelEn.setCardinfCurValid("");
                                    } else {
                                        icCardInfoExcelEn.setCardinfCurValid(entry.getValue().toString());
                                    }
                                }
                            }
                            list.add(icCardInfoExcelEn);
                        }
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(list);
    }

    //IC卡授权导表
    public Future<List<IcCardAuthExcelEn>> icCardAuthExportExcel(List<Map<String, Object>> fields, List<Map<String, Object>> ids, String filename,
                                                                 boolean isHeader, String mode, Boolean original, Map<String, String> pager, Map<String, String> form, String sheetName, HttpServletResponse response, String token, String plantId) throws Exception {
        List<Map<String, Object>> newIds = new ArrayList<>();
        List<IcCardAuthExcelEn> icCardAuthExcelEns = new ArrayList<>();//最终数据
        try {
            switch (mode) {
                case "current":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            Object object = field.get("field");
                            if (id.containsKey(object)) {
                                newId.put(object.toString(), id.get(object));
                            }
                        }
                        newIds.add(newId);
                    }
                    for (Map<String, Object> newId : newIds) {
                        IcCardAuthExcelEn icCardAuthEn = IcCardAuthExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            String key = entry.getKey();
                            if (key.equals("cardNo")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthCardNo(entry.getValue().toString());
                                }
                            }
                            if (key.equals("driverName")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthDriverName(entry.getValue().toString());
                                }
                            }
                            if (key.equals("cpyName")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthCpyName(entry.getValue().toString());
                                }
                            }
                            if (key.equals("operName")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthUser(entry.getValue().toString());
                                }
                            }
                            if (key.equals("state")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthStatus(entry.getValue().toString());
                                }
                            }
                            if (key.equals("createTime")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthCreatTime(entry.getValue().toString());
                                }
                            }
                            if (key.equals("startTime")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthStartTime(entry.getValue().toString());
                                }
                            }
                            if (key.equals("endTime")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthEndTime(entry.getValue().toString());
                                }
                            }
                        }
                        icCardAuthExcelEns.add(icCardAuthEn);
                    }
                    break;
                case "all":
//                    String plantId = null;
//                    if (token != null) {
//                        plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
//                    }
                    String icCardNo = form.get("icCardNo");
                    String startTime = form.get("startTime");
                    String endTime = form.get("endTime");
                    QueryWrapper<CIcCardsAuth> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("CARDAUTH_DELETED", 0);
                    queryWrapper.eq(icCardNo != null && !icCardNo.equals(""), "CARDAUTH_CARDNO", icCardNo);
                    queryWrapper.between(startTime != null && !startTime.equals("") && endTime != null && !endTime.equals(""), "CARDAUTH_CREATE_TIME", startTime, endTime);
                    queryWrapper.eq(plantId != null && !plantId.equals(""), "DATA_FROM", plantId);
                    queryWrapper.orderByDesc("CARDAUTH_CREATE_TIME");
                    List<CIcCardsAuth> icCardsAuthList = icCardsAuthService.list(queryWrapper);
                    for (CIcCardsAuth cIcCardsAuth : icCardsAuthList) {
                        CCompanys companys = companysService.getById(cIcCardsAuth.getCardauthCpyId());
                        IcCardAuthExcelEn icCardAuthEn = IcCardAuthExcelEn.builder()
                                .cardAuthCardNo(Optional.ofNullable((icCardsInfoService.getById(cIcCardsAuth.getCardauthCardId())).getCardinfCardno()).orElse(""))
                                .cardAuthDriverName(Optional.ofNullable((sysUserService.getById(cIcCardsAuth.getCardauthDriverUserid())).getTrueName()).orElse(""))
                                .cardAuthCpyName(Optional.of(companys.getCpyCo() + "." + companys.getCpyChico()).orElse(""))
                                .cardAuthUser(Optional.ofNullable((sysUserService.getById(cIcCardsAuth.getCardauthOperUserid())).getTrueName()).orElse(""))
                                .cardAuthStatus(Optional.ofNullable(IcCardAuthDic.getIcCardAuthDic(cIcCardsAuth.getCardauthState())).map(Object::toString).orElse(""))
                                .cardAuthCreatTime(Optional.ofNullable(cIcCardsAuth.getCardauthCreateTime()).map(Object::toString).orElse(""))
                                .cardAuthStartTime(Optional.ofNullable(cIcCardsAuth.getCardauthStartTime()).map(Object::toString).orElse(""))
                                .cardAuthEndTime(Optional.ofNullable(cIcCardsAuth.getCardauthEndTime()).map(Object::toString).orElse(""))
                                .build();
                        icCardAuthExcelEns.add(icCardAuthEn);
                    }
                    break;
                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
                    for (Map<String, Object> newId : newIds) {
                        IcCardAuthExcelEn icCardAuthEn = IcCardAuthExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            String key = entry.getKey();
                            if (key.equals("cardNo")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthCardNo(entry.getValue().toString());
                                }
                            }
                            if (key.equals("driverName")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthDriverName(entry.getValue().toString());
                                }
                            }
                            if (key.equals("cpyName")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthCpyName(entry.getValue().toString());
                                }
                            }
                            if (key.equals("operName")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthUser(entry.getValue().toString());
                                }
                            }
                            if (key.equals("state")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthStatus(entry.getValue().toString());
                                }
                            }
                            if (key.equals("createTime")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthCreatTime(entry.getValue().toString());
                                }
                            }
                            if (key.equals("startTime")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthStartTime(entry.getValue().toString());
                                }
                            }
                            if (key.equals("endTime")) {
                                if (entry.getValue() != null) {
                                    icCardAuthEn.setCardAuthEndTime(entry.getValue().toString());
                                }
                            }
                        }
                        icCardAuthExcelEns.add(icCardAuthEn);
                    }
                    break;
            }
            return new AsyncResult<>(icCardAuthExcelEns);
        } catch (Exception e) {
            log.error("导出ic卡授权信息失败", e);
            return null;
        }
    }


    public SzResult getCpyByICCardId(String id, String token, String plantId) throws Exception {
        // String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        plantId = "e9f6ed8e091611ec842700163e092c06";
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("PDTINF_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        CProductsInfo cProductsInfo = productsInfoService.getOne(queryWrapper);
        if (cProductsInfo == null) {
            return SzResult.error("该品种不存在或已被删除");
        }
        String pdtinfSubjectId = cProductsInfo.getPdtinfSubjectId();//品种绑定的单位
        QueryWrapper<CCompanys> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("ID", pdtinfSubjectId);
        queryWrapper3.eq("CPY_DELETED", 0);
        queryWrapper3.eq("DATA_FROM", plantId);
        CCompanys cCompanys = companysService.getOne(queryWrapper3);
        if (cCompanys == null) {
            return SzResult.error("该单位不存在或已被删除");
        } else {
            Map map = new HashMap<>();
            map.put("cpyCo", cCompanys.getCpyCo());
            map.put("cpyChico", cCompanys.getCpyChico());
            map.put("cpyId", cCompanys.getId());
            return SzResult.success(map);
        }
    }


}